<?php

/**
 * Description of Application
 *
 * @author nikolov
 */

namespace Shared\LRC\Service;

use Symfony\Component\DependencyInjection\ContainerInterface as Container;

class LRCSupport {

    private $CurrentLanguage;
    private $DefaultLanguage;

    public function __construct(Container $container) {
        $this->container = $container;
        $this->EntityManager = $container->get('doctrine')->getManager();
        $this->CurrentLanguage = $this->getCurrentLanguage();
    }

    public function setEntity($Entity) {
        $this->Entity = $Entity;

        $EntityParser = new \Shared\LRC\Metadata\Parsers\Entity($this->Entity, $this->EntityManager);
        $this->EntityMetadata = $EntityParser->parse();

        $TranslationParser = new \Shared\LRC\Metadata\Parsers\Translation($this->EntityMetadata->getTranslationEntityClass(), $this->EntityManager);
        $this->TranslationMetadata = $TranslationParser->parse();
    }

    public function getCurrentLanguage() {
        $Request = $this->container->get('request');
        if (!$this->CurrentLanguage) {
            $currentLocale = $Request->get('locale');

            if ($currentLocale) {
                $currentLocale = strtolower($currentLocale);

                if (!$this->CurrentLanguage = $this->EntityManager->getRepository('Shared\Entity\Language')->findOneByIsoCode($currentLocale)) {
                    throw new \Symfony\Component\HttpKernel\Exception\NotFoundHttpException('Language not found!');
                }
            } else {
                $this->CurrentLanguage = $this->getDefaultLanguage();
                if (!$this->CurrentLanguage) {
                    throw new \Exception('No default language found!');
                } else {
                    $Request->setLocale($this->CurrentLanguage->getIsoCode());
                }
            }
        }

        return $this->CurrentLanguage;
    }

    public function getDefaultLanguage() {
        if (!isset($this->DefaultLanguage)) {
            $this->DefaultLanguage = $this->EntityManager->getRepository('Shared\Entity\Language')->findOneByIsDefault(1);
        }
        return $this->DefaultLanguage;
    }

    public function setLanguage($isoCode) {
        $Language = $this->EntityManager->getRepository('Shared\Entity\Language')->findOneByIsoCode($isoCode);
        if ($Language) {
            $Request = $this->container->get('request');
            $Request->setLocale($Language->getIsoCode());
        }
    }

    public function set($Language, $property, $value) {
        $translationProperty = $this->EntityMetadata->getTranslationPropertyName();
        $objectProperty = $this->TranslationMetadata->getObjectPropertyName();
        $languageProperty = $this->TranslationMetadata->getLanguagePropertyName();

        $Collection = $this->Entity->{'get' . ucfirst($translationProperty)}();
        foreach ($Collection as $CurrentTranslation) {
            $TranslationLanguage = $CurrentTranslation->{'get' . ucfirst($languageProperty)}();
            if ($TranslationLanguage == $Language) {
                $Translation = $CurrentTranslation;
                break;
            }
        }
        $setMethod = 'set' . ucfirst($property);
        if (!isset($Translation) || !$Translation) {
            $classname = '\\' . $this->EntityMetadata->getTranslationEntityClass();
            $Translation = new $classname;
            $Translation->{'set' . ucfirst($languageProperty)}($Language);
            $Translation->{'set' . ucfirst($objectProperty)}($this->Entity);
            $this->EntityManager->persist($Translation);
            $Collection->add($Translation);
        }
        $Translation->{$setMethod}($value);

        if ($Language == $this->CurrentLanguage) {
            $this->Entity->{$setMethod}($value);
        }
    }

    public function setValues($property, $values) {
        foreach ($values as $languageId => $value) {
            $Language = $this->EntityManager->getReference('Shared\LRC\Entity\Language', $languageId);
            $this->set($Language, $property, $value);
        }
    }

}
